package com.nbufe.listener;

import com.github.pagehelper.PageInfo;
import com.nbufe.pojo.BookInfo;
import com.nbufe.pojo.TypeInfo;
import com.nbufe.service.BookInfoService;
import com.nbufe.service.TypeInfoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

/**
 * @Author Zan
 * @Create 2024/1/16 17:42
 * @Description :
 */
@WebListener
public class TypeInfoListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //手工从Spring容器中取出ProductTypeServiceImpl的对象.
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_*.xml");
        TypeInfoService typeInfoService = (TypeInfoService) context.getBean("typeInfoService");
        PageInfo<TypeInfo> pageInfo = typeInfoService.queryTypeInfoAll(null, 1, 100);
        List<TypeInfo> typeList = pageInfo.getList();
        //放入全局应用作用域中,供新增页面,修改页面,前台的查询功能提供全部商品类别集合
        servletContextEvent.getServletContext().setAttribute("typeList",typeList);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
